package com.blockmeta.bbs.immodule.activity.chat;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.net.callback.BaseResponseCallBack;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.dao.IMUserEntity;

/**
 * Created by author on 2019/3/25.
 * 客服聊天页面
 */
@Route(path = RouterPath.IM_CustomerServiceActivity_ACTIVITY)
public class CustomerServiceChatActivity extends BaseChatActivity {
    //arouter注入bug，只能在子类
    @Autowired(name = Constant.ZERO)
    public String ZeroParam;

    @Override
    protected void initialize() {

        super.initialize();
        mIvFriendOrCircleIcon.setImageResource(R.drawable.icon_friend);
        BindUserData();
    }

    @Override
    protected long getUserIdFromBundle() {
        if (!TextUtils.isEmpty(ZeroParam)) {
            return Long.parseLong(ZeroParam);
        } else {
            return getIntent().getLongExtra(Params.USER_TAG, 0);
        }
    }


    /**
     * 绑定服务号的信息，从数据库中获取
     */
    private void BindUserData() {
        getUserDataAndBind();
    }


    /**
     * 获取对方信息并绑定
     */
    private void getUserDataAndBind() {
        IMApplicationContext.getUserDomainService().getUser(toUserId, false, new BaseResponseCallBack<String, IMUserEntity>() {
            @Override
            public void fail(String msg) {
            }

            @Override
            public void success(IMUserEntity iMUserEntity) {
                mUser = iMUserEntity;
                bindUserData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mIsCreate) {
            mIsCreate = false;
        } else {
            //回到页面时需要更新名称
            getUserDataAndBind();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    private void bindUserData() {
        mTvTitle.setText(TextUtils.isEmpty(mUser.getRemarkName()) ? mUser.getName() : mUser.getRemarkName());
        mIvFriendOrCircleIcon.setVisibility(View.GONE);
    }


    @Override
    void jumpToFriendOrCircleHome() {
    }

    @Override
    protected int getChatType() {
        return ChatMessage.MSG_TYPE_UU;
    }
}
